﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BioCyber_Infection.Class
{
    class Stage22 : BioCyber_Infection.Class.Screens.Stages.Stage
    {
        public static Enemy[] enemyies = new Enemy[2];
        public static Enemy[] remoteEnemyies = new Enemy[2];
        private Vector2 teste = new Vector2(100, 100);

        GameWindow gameWindow;
        ContentManager content;

        #region Constructor
        public Stage22(ContentManager content, GameWindow gameWindow)
        {
            this.gameWindow = gameWindow;
            this.content = content;
            remoteEnemyies[0] = new Enemy(content, gameWindow, content.Load<Texture2D>("player2"));
        }
        #endregion


        #region DrawNetWorkSessions
        /// <DRAW SUMMARY>
        /// Método DRAW do Player.
        /// Tal método faz a leitura de todos que estão na Sessão,
        /// cria seus respectivos "Robôs"(players secundários) e chama
        /// o método DRAW de cada um deles.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //Vide sumário...
            foreach (NetworkGamer gamer in Network.networkSession.AllGamers)
            {
                Player player = gamer.Tag as Player;
                player.Draw(gamer,spriteBatch, content, gameTime);
                
                remoteEnemyies[0].Draw(gameTime, spriteBatch, remoteEnemyies[0].Stopped);
            }
        }
        #endregion


        #region
        public void Update(GameTime gameTime)
        {
            if (!LoadContent())
                LoadContent();
        }
        #endregion

        public override void SendPackages()
        {
            LocalNetworkGamer gamerHost = Network.networkSession.Host as LocalNetworkGamer;
            
            base.SendPlayerData();

            // update the other players with the current state of the local ship
            


            if (Network.networkSession.IsHost)
            {                
                //base.SendEnemyData(gamerHost,remoteEnemyies);
            }


            //base.SendPlayerData();
        }

        public override void ReceivePackages()
        {
            if (Network.networkSession != null && Network.networkSession.LocalGamers.Count > 0)
            {
                
                while (Network.networkSession.LocalGamers[0].IsDataAvailable)        
                {
                    NetworkGamer sender; 
                    Network.networkSession.LocalGamers[0].ReceiveData(Engine.packetReader, out sender);
                    PacketType packetType = (PacketType)Engine.packetReader.ReadInt32();

                    switch (packetType)
                    {
                        case(PacketType.PLAYERDATA):
                            if (sender != null && !sender.IsLocal)
                            {
                                base.UpdatePlayerData(sender);
                            }
                            break;

                        case(PacketType.STAGEDATA):
                            if (!(Network.networkSession.IsHost))
                            {
                                //base.UpdateEnemyData(remoteEnemyies);
                            }
                            break;
                    }
                }
            }
        }
           
        

    }

}
